//
//  MySearchBarView.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/8/2.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "MySearchBarView.h"

@interface MySearchBarView ()<UITextFieldDelegate>


//@property(nonatomic,strong)UIButton *allButton;

@end

@implementation MySearchBarView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        self.borderStyle = UITextBorderStyleRoundedRect;
        //设置输入框内容的字体样式和大小
        self.font = [UIFont fontWithName:@"Arial" size:11.0f * FitH];
//        self.textColor  = [UIColor blackColor];
        self.layer.cornerRadius = 15 * FitH;
        self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索你想要的商品" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"333333"],NSFontAttributeName:[UIFont systemFontOfSize:11]}];

//        self.keyboardType = UIKeyboardTypeWebSearch;
        self.returnKeyType = UIReturnKeySearch;
        
        self.intputView = [[UIView alloc] init];
        self.intputView.backgroundColor = [UIColor clearColor];
        self.intputView.frame= CGRectMake(0, 0 ,60 * FitW  , 30 * FitH);
        // 把leftVw设置给文本框
        self.leftView = self.intputView;
        self.leftViewMode = UITextFieldViewModeAlways;
        self.delegate = self;
        
//        self.rightViewMode = UITextFieldViewModeWhileEditing;
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        
        UIImageView *showImg = [[UIImageView alloc]init];
        showImg.image = LoadImage(@"down");
        [self.intputView addSubview:showImg];
        [showImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.intputView.mas_right).offset(-5 * FitW);
            make.centerY.equalTo(self.intputView.mas_centerY);
            make.size.mas_offset(CGSizeMake(5 * FitW, 3 * FitH));
        }];
        
        
        
        self.allButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.allButton.tag = 2;
//        self.allButton.
        [_allButton setTintColor:[UIColor blackColor]];
                _allButton.backgroundColor = [UIColor clearColor];
        self.allButton.titleLabel.text = @"全部";
        [self.allButton setTitle:@"全部" forState:UIControlStateNormal];
        self.allButton.titleLabel.font = [UIFont systemFontOfSize:14 * FitH];
        [self.intputView addSubview:self.allButton];
        [self.allButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.intputView.mas_left).offset(15 * FitW);
            make.right.equalTo(showImg.mas_left).offset(-5 * FitW);
            make.centerY.equalTo(showImg.mas_centerY);
            make.height.mas_equalTo(14 * FitH);
//            make.width.mas_offset(30 * FitW);
        }];
        
        
        
        
       
        
    }
    return self;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
//    self.rightView = nil;
//    self.leftView = self.allButton;
//    self.leftViewMode = UITextFieldViewModeAlways;
//    self.clearButtonMode = UITextFieldViewModeAlways;
}

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    self.leftView = nil;
//    self.rightView = self.imgSearch;
//    self.rightViewMode = UITextFieldViewModeAlways;
//
//    [self resignFirstResponder];
//}

//-(void)buttonClickedAction:(UIButton *)sender
//{
//    if (self.buttonClickHandler)
//    {
//        self.buttonClickHandler();
//    }
//}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//    DLog(@"点击键盘搜索");
//    [self resignFirstResponder];
    
    [self.keyBoardDelegate clickedAction:textField.text];//键盘点击代理事件
    
    return YES;
}

@end
